//
//  DWJWelComeViewController.swift
//  weibo
//
//  Created by 代伟佳 on 16/4/19.
//  Copyright © 2016年 代伟佳. All rights reserved.
//

import UIKit
import SDWebImage

class DWJWelComeViewController: UIViewController {

    override func loadView() {
        
        view = bgImageView
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        setHeadImageViewAnim()
    }
    
    private func setHeadImageViewAnim() {
    
        // 更新headImageView的约束
        headImageView.snp_updateConstraints { (make) -> Void in
            
            make.top.equalTo(view).offset(100)
        }
        
        // 阻尼动画
        // 1. 动画执行时长  2. 延迟多久调用动画  3. 阻尼0-1之间阻尼越小弹性效果越小  4.起始速度
        UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            // 需要刷新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    
                    self.messaheLabel.alpha = 1
                    
                }, completion: { (_) -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName("switchDeflutVcNSNotification", object: "welCome")
        
                })
            }

    }
    
    
    
    private func setUpUI() {
    
        view.addSubview(headImageView)
        view.addSubview(messaheLabel)
    
        headImageView.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        messaheLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
        
    }
    
    // 背景图
    private lazy var bgImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
        
        return imageView
    }()
    
    // 用户头像
    private lazy var headImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 85 / 2
        imageView.layer.borderColor = UIColor.orangeColor().CGColor
        imageView.layer.borderWidth = 2
        imageView.clipsToBounds = true
        imageView.sd_setImageWithURL(NSURL(string: DWJUersAccountTools.sharedTools.userAccount?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        return imageView
    }()
    
    // 欢迎回来
    private lazy var messaheLabel: UILabel = {
    
        let label = UILabel(textColor: UIColor.darkGrayColor(), textFont: 17)
        label.textAlignment = NSTextAlignment.Center
        label.text = "欢迎回来"
        label.alpha = 0
        return label
    }()

}
